<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
use Model\StudentModel;

class ManagerController extends Controller {
    function login() {
    	if (!empty($_POST)) {
    		$ver = new Verify();
    		if (!$ver->check($_POST['code'])) {
    			$error = '验证码错误';
    			$this->assign('error',$error);
    		} else {
    			$student = D('Student')->where('t_number=' . $_POST['t_number'])->find();
    			if ($student) {
    				if ($student['id_number'] == $_POST['id_number']) {
    					if ($student['password'] == MD5($_POST['password'])) {
    						session('t_number',$student['t_number']);
    						session('name',$student['name']);
    						$this->redirect('Index/index');
    					} else {
    						$error = '输入密码不正确，请核对后重试！';
    						$this->assign('error',$error);
    					}
    				} else {
	    				$error = '身份证号码不正确，请核对后重试！';
	    				$this->assign('error',$error);
	    			}
    			} else {
    				$error = '准考证号不正确，请核对后重试！';
    				$this->assign('error',$error);
    			}
    		}
    	}
    	$this->display();
    }
    //验证码
    function verifyImg() {
    	$user = new \Model\UserModel();
    	$user->verify();
    }
    
    function register() {
    	$student = new \Model\StudentModel();
    	$teacher = D('Manager')->where('role=51')->select();
    	$this->assign('teacher',$teacher);
    	if (!empty($_POST)){
    		if (!$student->create()) {
    			$error = $student->getError();
    			$this->assign('error',$error);
    		} else {
    			$_POST['password'] = md5($_POST['password']);
    			$z = $student->add($_POST);
    			if ($z) {
    				$this->redirect('Tips/tips');
    			}
    		}
    	}
    	$this->display();
    }
    
    function acticle() {
    	$acticle = D('Acticle')->where('id=1')->find();
    	$this->assign('acticle',$acticle);
    	$this->display();
    }
    
    function upd() {
    	$student = new \Model\StudentModel();
    	if (!empty($_POST)){
    		if (!$student->create()) {
    			$error = $student->getError();
    			$this->assign('error',$error);
    		} else {
    			$id = $_POST['id'];
    			$sql = "select * from ts_student where id_number= '$id'";
    			$pass = $student->query($sql);

    			if ($pass) {
    				if (md5($_POST['oldpass']) == $pass[0]['password']) {
    					$_POST['password'] = md5($_POST['password']);
    					$student->password = $_POST['password'];
    					$z = $student->where('id_number=' . $_POST['id'])->save();
    					if ($z) {
    						$this->redirect('Tips/tips');
    					}
    				} else {
    					$error_t = '原密码错误，请重试！';
    					$this->assign('error_t',$error_t);
    				}
    			} else {
    				$error_f = '身份证信息错误，请重试！';
    				$this->assign('error_f',$error_f);
    			}
    		}
    	}
    	$this->display();
    }
    
    function mark() {
    	if (!empty($_POST['t_number'])) {
    		$mark = D('Student')->where('t_number=' . $_POST['t_number'])->find();
    		$this->assign('error',$mark['mark']);
    	}
    	$this->display();
    }
}